﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Data;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Kmust.App_Data.KmustDataTableAdapters;
using Kmust.Commom;

namespace Kmust.KmustControl
{
    /// <summary>
    /// StudentControl.xaml 的交互逻辑
    /// </summary>
    public partial class StudentControl : UserControl
    {
       
        //
        Kmust.App_Data.KmustData.XSDataTable xsd = new App_Data.KmustData.XSDataTable();
        KmustPager Page = new KmustPager();
        private void initDate()
        {
            using (XSTableAdapter xs = new XSTableAdapter())
            {
                xsd = xs.GetData();
                
            }
        }

        //UI数据更新
        public void upDate(string value)
        {
             //page.NMax=xsd.TableName("XS").
           Page.NMax= xsd.Count(row => row.ZZJGM.Equals(value));
          // Page.PageCurrent = 1;
           AllCountLabel.Content = "总记录数：" + Page.NMax.ToString();
           CountLabel.Content = "总页数: " + Page.PageCount;
           CurrentLabel.Content = "当前页:" + (Page.PageCurrent+1).ToString();
        }
        private string setsource;

        public string setSource
        {
            get { return setsource; }
            set {
                if (value != null)
                {
                    setsource = value;
                  // StudentGrid.ItemsSource =xsd.Where(row=>row.ZZJGM.Equals(value)).Take(Page.PageSize);
                    //当前页置零
                    Page.PageCurrent = 0;
                    //即第一页
                    setNewSource();
                  //  MessageBox.Show(value);
                }
            }
        }
        //定义委托
       // public delegate void SetSource(object send, object arg);

        //自定义属性
        public static readonly DependencyProperty setSourceProperty=DependencyProperty.Register("setSource",typeof(string),typeof(StudentControl),null);

      //  public event SetSource setSource;

        public StudentControl()
        {
            InitializeComponent();
            initDate();
           // setSource += StudentControl_setSource;
        }

        //void StudentControl_setSource(object send, object arg)
        //{
        //    //throw new NotImplementedException();
        //    try
        //    {
        //        //StudentGrid.ItemsSource = null;
        //        //XSTableAdapter xs = new XSTableAdapter();
        //        //StudentGrid.ItemsSource = xs.GetData().Take(5);
        //        //this.r
        //        ///return true;
        //       // StudentGrid.r
        //        MessageBox.Show((string)arg);
        //    }
        //    catch (Exception e)
        //    {
        //      //  return false;
        //    }

        //}

        

        //private void StudentGrid_Load(object sender, RoutedEventArgs e)
        //{
        //  //  XSTableAdapter xs = new XSTableAdapter();
        //  //  StudentGrid.ItemsSource = xs.GetData().Take(5);
        //}


        private void CheckBox_Click_1(object sender, RoutedEventArgs e)
        {
            if ((bool)(e.OriginalSource as CheckBox).IsChecked)
            {
                Student_Image.Source = new BitmapImage(new Uri("../Resource/two.jpg", UriKind.Relative));
            }
            else
            {
                Student_Image.Source = null;
            }
        }

        private void LastPage_Click(object sender, RoutedEventArgs e)
        {
            if (Page.PageCurrent == 0)
            {
                MessageBox.Show("没有上一页");
            }
            else if(Page.PageCurrent>=1)
            {
                Page.PageCurrent = Page.PageCurrent - 1;
                setNewSource();
            }
        }

        private void NextPage_Click(object sender, RoutedEventArgs e)
        {
            if (Page.PageCount < 1)
            {
                MessageBox.Show("没有下一页");
            }
            else if ( Page.PageCount == (Page.PageCurrent+1))
            {
                MessageBox.Show("以及没有下一页了");
            }
            else if ((Page.PageCurrent + 1) < Page.PageCount)
            {
                Page.PageCurrent = Page.PageCurrent + 1;
                setNewSource();
            }
        }

        private void setNewSource()
        {
            StudentGrid.ItemsSource = xsd.Where(row => row.ZZJGM.Equals(setSource)).Skip(Page.PageCurrent * Page.PageSize).Take(Page.PageSize);
            upDate(setSource);
            this.UpdateLayout();
        }
    }
}
